package com.shop.zkmall.app.service.a.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.zkmall.app.domain.CereBuyerCoupon;
import com.shop.zkmall.app.domain.req.AddCoupon;
import com.shop.zkmall.app.mapper.CereBuyerCouponMapper;
import com.shop.zkmall.app.service.a.ICereBuyerCouponService;
import com.shop.zkmall.app.utils.R;
import com.shop.zkmall.commons.result.Result;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class CereBuyerCouponServiceImpl extends ServiceImpl<CereBuyerCouponMapper, CereBuyerCoupon> implements ICereBuyerCouponService {
    @Override
    public R<String> addCoupon(AddCoupon addCoupon) {
        CereBuyerCoupon cereBuyerCoupon = new CereBuyerCoupon();
        cereBuyerCoupon.setCouponType(addCoupon.getCouponType());
        cereBuyerCoupon.setCouponName("普通券");
        cereBuyerCoupon.setAmount(addCoupon.getAmount());
        cereBuyerCoupon.setMinAmount(addCoupon.getFullAmount());
        cereBuyerCoupon.setValidType(1);
        cereBuyerCoupon.setValidDays(addCoupon.getEffectiveDay());
        LocalDateTime now = DateUtil.toLocalDateTime(new Date());
        cereBuyerCoupon.setValidStart(now);
        Integer effectiveDay = addCoupon.getEffectiveDay();
        LocalDateTime plusOneDay = now.plusDays(effectiveDay);
        cereBuyerCoupon.setValidEnd(plusOneDay);
        save(cereBuyerCoupon);
        return R.ok();
    }
}
